package hw.ats.news.activity.etc;

import hw.ats.news.R;
import hw.ats.news.activity.DBAwareActivity;
import hw.ats.news.activity.news.BookmarkAct;
import hw.ats.news.activity.news.SearchAct;
import hw.ats.news.ui.BottomTabHostProvider;
import hw.ats.news.ui.CommonUI;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.humanwares.ui.tabbar.TabHost;
import com.tattermedia.allthataudio.audio.AudioPlayerManager;
import com.tattermedia.allthataudio.audio.CommonAudioPlayerStatusListener;
import com.tattermedia.allthataudio.audio.IAudioPlayerStatusListener;
import com.tattermedia.allthataudio.views.AudioPlayerProgressView;

public class MoreAct extends DBAwareActivity {
	private ListView cntView = null;
	private List<More> cntItems = null;
	private TabHost navbar = null;
	
	// audio status listener
	private IAudioPlayerStatusListener		mAudioStatusListener;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.act_more);
        
        // 탭바 초기화
        navbar = CommonUI.renderTabBar(this, BottomTabHostProvider.TAB5);
		
		// 네비게이션바 제목
        CommonUI.renderAppTitle(this);
        
        // 좌측 네비게이션 버튼 = 검색화면이동
        CommonUI.addLeftNavigationButtonBackEvent(this);
        
        cntItems = new ArrayList<More>();
        
        // 그외 메뉴 출력
//        cntItems.add(new More(R.drawable.ic_more_bookmark, "북마크", BookmarkAct.class, 0));
        cntItems.add(new More(R.drawable.ic_more_search, "검색", SearchAct.class, 0));
        cntItems.add(new More(R.drawable.ic_more_app_recmd, "친구들에게 앱 추천하기", RecmdAppAct.class, 0));
        cntItems.add(new More(R.drawable.ic_more_settings, "설정", SettingsAct.class, 0));
        cntItems.add(new More(R.drawable.ic_more_about, "정보", AboutAct.class, 0));
        
        cntView = (ListView)findViewById(R.id.more_list);
        cntView.setAdapter(new MoreArrayAdapter(
				this, R.layout.row_more, cntItems));
        cntView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				More more = cntItems.get(position);
				if (SettingsAct.class.getName().equals(more.activity.getName())) {
					startActivity(new Intent(MoreAct.this, SettingsAct.class));
					return;
				} else if (RecmdAppAct.class.getName().equals(more.activity.getName())) {
			    	Intent intent = new Intent(MoreAct.this, more.activity);
			    	intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
			    	startActivity(intent);
			    	return;
				}
				
		    	Intent intent = new Intent(MoreAct.this, more.activity);
		    	intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
		    	if (more.cate_uid > 0) {
		    	   	Bundle extras = new Bundle();
		        	extras.putInt("cate_uid", more.cate_uid);
		        	intent.putExtras(extras);
		    	}
		    	startActivity(intent);
			}
		});
        
		
        // add audio status listener
        mAudioStatusListener = new CommonAudioPlayerStatusListener(
        		(ImageButton)findViewById(R.id.audio_player_button_play), 
        		(ImageButton)findViewById(R.id.audio_player_button_repeat),
        		(ImageButton)findViewById(R.id.audio_player_button_prev),
        		(ImageButton)findViewById(R.id.audio_player_button_next),
        		(TextView)findViewById(R.id.audio_player_text_subject),
        		(TextView)findViewById(R.id.audio_player_text_time),
        		(AudioPlayerProgressView)findViewById(R.id.audio_player_progress_view));
        
        AudioPlayerManager.getManager().addAudioPlayerStatusListener(mAudioStatusListener);
    }
    
    @Override
	protected void onDestroy() {
		super.onDestroy();
		
		AudioPlayerManager.getManager().removeAudioPlayerStatusListener(mAudioStatusListener);
		
		closeDatabase();
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        int visibility = navbar.getVisibility();
        navbar.setVisibility(visibility == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
        return false;
    }
    
    class MoreArrayAdapter extends ArrayAdapter<More> {
        private List<More> items = null;

        public MoreArrayAdapter(Context context, int textViewResourceId, 
        		List<More> items) {
    		super(context, textViewResourceId, items);
    		this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getContext().getSystemService(
                		Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row_more, null);
            }
            
            More more = items.get(position);
            
            ImageView _img = (ImageView)v.findViewById(R.id.row_photo);
            if (_img != null) {
            	_img.setImageResource(more.thumb);
            }
            
            TextView _text = (TextView)v.findViewById(R.id.row_title);
            if (_text != null && more.title != null) {
            	_text.setText(more.title);
            }
            
    		return v;
        }
    }
    
    class More {
    	public int thumb = 0;
    	public String title = null;
    	@SuppressWarnings("unchecked")
		public Class activity = null;
    	public int cate_uid = 0;
    	
    	@SuppressWarnings("unchecked")
		public More(int thumb, String title, Class activity, int cate_uid) {
    		this.thumb = thumb;
    		this.title = title;
    		this.activity = activity;
    		this.cate_uid = cate_uid;
    	}
    }
}
